import locale from '@fullcalendar/core/locales/zh-cn'
import dayGridPlugin from '@fullcalendar/daygrid'
import type { CalendarOptions } from '@fullcalendar/core'
import type {
  TeamsInfo,
  TeamsQueryListParams,
  TeamsSaveOrUpdateParams,
} from '@/types'
import { PAGING } from '@/constant'

export const defaultFormData: Partial<TeamsSaveOrUpdateParams>
  = {
    indicatorCode: '',
    teamsName: '',
    startTime: '',
    endTime: '',
    pointId: null as any,
  }

export const formRules
  = generateCurrencyFormRules(defaultFormData)

export const defaultPagingParams: Partial<TeamsQueryListParams>
  = {
    ...PAGING.PARAMS,
    indicatorCode: '',
    teamsName: '',
    month: '',
    timeSection: '',
    endTime: '',
    startTime: '',
  }

export function disabledTime() {
  const arrs: number[] = []
  for (const i of range(60)) {
    arrs.push(i)
  }
  return arrs
}

export function handle(
  rawData: Record<string, TeamsInfo[]>,
) {
  const m: Record<string, any> = {}
  for (const key in rawData) {
    const maxDate = Math.max(
      ...rawData[key]!.map(item =>
        new Date(`${item.endTime}:00`).getTime(),
      ),
    )
    const data
      = rawData[key]?.map((item) => {
        const startTime = `${item.startTime}:00`
        const endTime = `${item.endTime}:00`
        const span = intervalDays(startTime, endTime)
        const title = `${item.teamsName}: ${
          item.indicatorCode
        } ${formatTime(
          startTime,
          'MM月DD hh:00',
        )}--${formatTime(endTime, 'MM月DD hh:00')}`
        const n = {
          span,
          title,
        }
        return n
      }) ?? []
    m[key] = {
      maxSpan: intervalDays(key, maxDate),
      data: data.sort((a, b) => b.span - a.span),
    }
  }
  console.warn('看我：', m)
  return m
}

export const calendarOptions: CalendarOptions = {
  timeZone: 'UTC',
  initialView: 'dayGridMonth',
  plugins: [dayGridPlugin], // 需要加载的插件
  locale, // 语言汉化
  weekends: true, // 是否显示周末，设为false则不显示周六和周日
  selectable: true, // 是否可以选中日历格
  headerToolbar: {
    left: 'prevYear,prev,next,nextYear today',
    center: 'title',
    right: 'dayGridMonth,dayGridWeek,dayGridDay',
  },
  // customButtons:{},
  // headerToolbar: false, // Fullcalendar表头显示区域不显示，显示自己自定义的html头部
  // 使用内置按钮的显示文本
  // buttonText: {
  //   today: 'cs'
  // },
  // headerToolbar: false,
  editable: true,
  events:
    'https://fullcalendar.io/api/demo-feeds/events.json',
}
